package pers.cz.logtail;

import org.junit.Test;

/**
 * @program: JefLogTail
 * @description:
 * @author: Cheng Zhi
 * @create: 2023-10-04 20:16
 **/
public class Main {

    private static final String ZOOKEEPER_ADDRESS = "106.225.210.32:2181";
    private static final String SERVICE_NAME = "fanqiechaodan";

    public static void main(String[] args) throws Exception {
        // 创建服务注册对象
        ServiceRegistByZk serviceRegistry = new ServiceRegistByZk(ZOOKEEPER_ADDRESS);


        // 启动线程1，进行服务的注册
        Thread thread1 = new Thread(() -> {
            try {
                serviceRegistry.registryService(SERVICE_NAME, "127.0.0.1:8080");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        thread1.start();

        // 让线程1先执行一会儿，以确保服务已经被注册
        Thread.sleep(3000);

        // 创建服务消费对象
        ServiceDiscoveryByZk serviceConsumer = new ServiceDiscoveryByZk(ZOOKEEPER_ADDRESS);

        // 启动线程2，进行服务的消费
        Thread thread2 = new Thread(() -> {
            try {
                String serviceAddress = serviceConsumer.consumeService(SERVICE_NAME);
                System.out.println("Service consumed: " + serviceAddress);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        thread2.start();

        // 等待线程2执行完成
        thread2.join();

        // 睡眠30秒查看ZooKeeper节点是否存在
        Thread.sleep(30000);
        // 关闭服务注册和服务消费对象
        serviceRegistry.close();
        serviceConsumer.close();
    }

    /**
     * @author Chengzhi
     * @date 2023-10-04
     * @测试目的：
     * @预期结果:
     */
    @Test
    public void test(){

    }

}
